package com.blockmeta.bbs.immodule.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.corebean.IMMetaBase;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.eventbus.UserInfoChangeEvent;
import com.blockmeta.bbs.immodule.net.api.GetRemarkNameApi;
import com.blockmeta.bbs.immodule.net.api.RemarkFriendApi;
import com.blockmeta.bbs.immodule.net.api.RemarkMyNameInGroup;
import com.blockmeta.bbs.immodule.widget.LengthLimitEditText;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.blockmeta.bbs.immodule.bean.Params.REMARK;

/**
 * @author airthor
 * @date 2019/2/21
 * 备注好友
 */
public class RemarksActivity extends BaseActivity {

    @BindView(R2.id.edt_circle_info)
    LengthLimitEditText mEdtCircleInfo;
    @BindView(R2.id.btn_save)
    Button mBtnSave;

    private long mFriendId;

    private long mCircleId;
    private String mRemarkName = "";

    @Override
    protected boolean showTitle() {
        return false;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_remarks,null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mFriendId = getIntent().getLongExtra(Params.USERID, 0);
        mCircleId = getIntent().getLongExtra(Params.CIRCLE_ID, 0);
        mRemarkName = getIntent().getStringExtra(Params.MYCIRCLEREMARK);


        if(mFriendId != 0){
            getFriendRemark();
        }else {
            getMyCircleRemarkName();
        }
    }

    private void getMyCircleRemarkName() {
        if(!TextUtils.isEmpty(mRemarkName)){
            mEdtCircleInfo.setText(mRemarkName);
            mEdtCircleInfo.setSelection(mRemarkName.length());
        }
    }

    private void getFriendRemark() {
        ProgressBarUtil.showProgress(mContext);
        RetrofitFactory.getRetrofit().create(GetRemarkNameApi.class)
                .getRemarkName(mFriendId , AccountHelper.getUidInt())
                .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                .compose(RxUtil.<Object>commonResult())
                .compose(((RxAppCompatActivity)mContext).<Object>bindToLifecycle())
                .subscribe(new CommonSubscriber<Object>(mContext) {
                    @Override
                    public void onNext(Object o) {
                        if(o != null && o instanceof String) {
                            mEdtCircleInfo.setText((String)o);
                            mEdtCircleInfo.setSelection(((String) o).length());
                        }

                    }
                });
    }

    @OnClick({R2.id.fl_back, R2.id.fl_edit, R2.id.btn_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.fl_back) {
            finish();

        } else if (i == R.id.btn_save) {
            if(mFriendId != 0){
                remarkFriend(mFriendId);
            }else if(mCircleId != 0){
                remarkMyCircleName(mCircleId);
            }
        } else {
        }
    }

    /**
     * 备注好友
     * @param friendId 要备注的好友ID
     */
    private void remarkFriend(final long friendId) {
        if(friendId == 0){
            ToastUtil.show("无效id！");
            return;
        }
        ProgressBarUtil.showProgress(mContext);
        RetrofitFactory.getRetrofit().create(RemarkFriendApi.class)
                .remarkFriend(AccountHelper.getUidInt(),friendId,mEdtCircleInfo.getText().toString().trim())
                .compose(RxUtil.dataNullResult())
                .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                .compose(((RxAppCompatActivity)mContext).<BaseImResponse<Object>>bindToLifecycle())
                .subscribe(new CommonSubscriber<BaseImResponse<Object>>(mContext) {
                    @Override
                    public void onNext(BaseImResponse<Object> o) {
                            ToastUtil.show("好友备注已修改");
                            //修改数据库中的好友的备注信息
                            IMApplicationContext.getUserDomainService().changeUserRemarkName(mEdtCircleInfo.getText().toString().trim(), friendId);
                            Map<String, String> notifyMap = new HashMap<>(1);
                            notifyMap.put(REMARK, mEdtCircleInfo.getText().toString().trim());
                            IMApplicationContext.getIMMetaService().post(new UserInfoChangeEvent(mFriendId, notifyMap));
                            finish();
                    }
                });
    }

    private void remarkMyCircleName(final long circleId) {
        ProgressBarUtil.showProgress(mContext);
        final String remarkName = mEdtCircleInfo.getText().toString().trim();
        RetrofitFactory.getRootRetrofit().create(RemarkMyNameInGroup.class)
                .updateMemberRemarkName(circleId, remarkName)
                .compose(RxUtil.dataNullResult())
                .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                .compose(((RxAppCompatActivity)mContext).<BaseImResponse<Object>>bindToLifecycle())
                .subscribe(new CommonSubscriber<BaseImResponse<Object>>(mContext) {
                    @Override
                    public void onNext(BaseImResponse<Object> o) {
                        ToastUtil.show("已修改");
                        IMApplicationContext.getCircleDomainService().updateMyCircleRemarkName(mCircleId, remarkName);
                        IMApplicationContext.getIMMetaService().post(new IMMetaBase.Event_MyCircleRemarkNameChange(mCircleId));
                        finish();
                    }
                });
    }
}
